package thread;

import java.util.concurrent.CountDownLatch;

public class demo44 {
    public static void main(String[] args) throws InterruptedException {
        //初始化的参数，就是任务的个数
        CountDownLatch countDownLatch = new CountDownLatch(8);

        for (int i = 0; i < 8; i++) {
            int id = i;
            Thread t = new Thread(()->{
                //假设线程进行一定的工作，此处就通过sleep来模拟
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
                System.out.println("线程" + id + "执行完毕");
                //相当于运动员到达终点
                countDownLatch.countDown();
            });
            t.start();
        }

        //主线程等待所有的线程都撞线
        // a 理解为 all 的意思
        // await阻塞，知道countDownLatch 调用 countDown 的次数和初始化值相同
        countDownLatch.await();
        System.out.println("所有任务都执行完毕");
    }
}
